import 'dart:math';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_tools/chart_tools.dart';
import 'package:x3_charts_core/model/multi_region_pie_chart_model.dart';

///**
///* @author hrongyong
///* @date   2/21/21
///* @email hrongyong@gmail.com
///* Description: 多区域饼图
///*/
class MultiRegionPieChartPainter extends CustomPainter {
  List<MultiRegionPieChartModel> chartList;

  final double titleFontSize;

  MultiRegionPieChartPainter({@required this.chartList, this.titleFontSize = 10, this.leftAndRightMargins = 15.0}) {
    areas = chartList.length;
  }

  ///最大半径
  double maxRadius = 0.0;

  ///多少区域
  int areas = 0;

  ///左右边距
  double leftAndRightMargins;

  Paint fillPaint = Paint();

  ///圆的度数
  final double CIRCLE_ANGLE = 360;

  ///文字
  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);
  final TextPainter _subTextPainter = TextPainter(textDirection: TextDirection.ltr);

  @override
  void paint(Canvas canvas, Size size) {
    /// 确定上下文字
    TextPainter painter01 = TextPainter(
        maxLines: 1,
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: "test",
            style: TextStyle(
              fontSize: titleFontSize,
            )));
    painter01.layout();

    ///确定最大半径
    maxRadius = min(size.width / 2, (size.height - painter01.height * 2) / 2) - leftAndRightMargins;
    maxRadius = maxRadius * 0.9;

    ///移动画布中心到size中心
    canvas.translate(size.width / 2, size.height / 2);

    ///绘制饼图
    drawPie(canvas, size);

    ///绘制文字
    drawText(canvas, size);
  }

  drawText(Canvas canvas, Size size) {
    canvas.save();

    ///累计的角度
    double grandTotal = 0.0;

    for (int i = 0; i < chartList.length; i++) {
      if (i != 0) {
        grandTotal += CIRCLE_ANGLE * chartList[i - 1].percentage;
      }

      double x = maxRadius * sin(ChartTools.degToRad(grandTotal + (CIRCLE_ANGLE * chartList[i].percentage) / 2));
      double y = maxRadius * cos(ChartTools.degToRad(grandTotal + (CIRCLE_ANGLE * chartList[i].percentage) / 2));
      _drawAxisText(canvas, size, offset: Offset(x, -y), title: chartList[i].title, subTitle: chartList[i].subTitle, color: Colors.black);
    }
    grandTotal = 0.0;
    canvas.restore();
  }

  void _drawAxisText(
    Canvas canvas,
    Size size, {
    String title,
    String subTitle,
    Offset offset = Offset.zero,
    Color color,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: titleFontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;

    ///绘制副标题
    TextSpan subText = TextSpan(text: subTitle, style: TextStyle(fontSize: titleFontSize, color: color));
    _subTextPainter.text = subText;
    _subTextPainter.layout(); // 进行布局
    Size subTextSize = _subTextPainter.size;
    Offset textOffsetPos = ChartTools.roundStructureTextOffset(offset: offset, textSize: textSize, subTextSize: subTextSize)[0];
    Offset subTextOffsetPos = ChartTools.roundStructureTextOffset(offset: offset, textSize: textSize, subTextSize: subTextSize)[1];

    _textPainter.paint(canvas, textOffsetPos);
    _subTextPainter.paint(canvas, subTextOffsetPos);
  }

  drawPie(Canvas canvas, Size size) {
    canvas.save();
    // 由于希望一开始从 Y 轴负向开始绘制，可以使用 canvas.rotate(-pi / 2); 操作画布。
    canvas.rotate(-pi / 2);
    for (int i = 0; i < chartList.length; i++) {
      Color color = chartList[i % chartList.length].color;
      if (chartList[i].percentage == 1.0) {
        canvas.drawCircle(
            Offset.zero,
            maxRadius,
            fillPaint
              ..style = PaintingStyle.fill
              ..color = color);
      } else {
        Path path = Path();
        path.lineTo(maxRadius, 0);
        path.arcTo(Rect.fromCenter(center: Offset.zero, width: maxRadius * 2, height: maxRadius * 2), 0, 2 * pi * chartList[i].percentage, false);
        path.close();
        canvas.drawPath(
            path,
            fillPaint
              ..style = PaintingStyle.fill
              ..color = color);
      }

      canvas.rotate(2 * pi * chartList[i].percentage);
    }

    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
